package com.shixun.milktea.service.impl;

import com.shixun.milktea.entity.Address;
import com.shixun.milktea.enums.ResultStatusEnum;
import com.shixun.milktea.handler.GlobalException;
import com.shixun.milktea.mapper.AddressMapper;
import com.shixun.milktea.service.AddressService;
import com.shixun.milktea.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;

/**
 * @author : hzc
 * @date: 2022/7/1 - 07 - 01 - 19:19
 * @Description: com.shixun.milktea.service.impl
 * @version: 1.0
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    @Transactional
    public Integer addOneAddress(Address address) {
        if(address.getAddressStatus()==null||address.getAddressStatus().length()==0){
            throw new GlobalException(ResultStatusEnum.DATA_WRONG);
        }else if(address.getAddressStatus().equals("1")){
            //设置为默认地址
            addressMapper.setDefaultStatus(address.getConsumerId());
        }
        return addressMapper.addOneAddress(address);
    }

    @Override
    public List<Address> getAddressByConsumerId(Integer consumerId) {
        return addressMapper.findAddressByConsumerId(consumerId);
    }

    @Override
    @Transactional
    public Integer updateOneAddress(Address address) {
        if(address.getAddressStatus()==null||address.getAddressStatus().length()==0){
            throw new GlobalException(ResultStatusEnum.DATA_WRONG);
        }else if(address.getAddressStatus().equals("1")){
            //设置为默认地址
            addressMapper.setDefaultStatus(address.getConsumerId());
        }
        return addressMapper.updateOneAddress(address);
    }

    @Override
    public Integer deleteAddressByAddressId(Integer addressId) {
        return addressMapper.updateStatusById(addressId,"-1");
    }

}
